﻿//中文验证提示
jQuery.extend(jQuery.validator.messages, {
    required: "必填字段",
    remote: "输入不正确",
    email: "邮箱地址不正确",
    url: "网址格式不正确",
    date: "日期格式不正确",
    dateISO: "日期格式不正确",
    number: "请输入数字",
    digits: "请输入整数",
    creditcard: "卡号格式不正确",
    equalTo: "重复输入错误",
    accept: "后缀不正确",
    maxlength: jQuery.validator.format("最多{0}个字符"),
    minlength: jQuery.validator.format("最少{0}个字符"),
    rangelength: jQuery.validator.format("请输入{0}到{1}个字符"),
    range: jQuery.validator.format("{0}到{1}之间的数字"),
    max: jQuery.validator.format("最大值{0}"),
    min: jQuery.validator.format("最小值{0}")
});

//扩展验证方法：必填且不等于提示文字
jQuery.validator.addMethod(
    "required_cue",
    function (value, element, params) {
        if (value == "") {
            return false;
        }
        return value != params;
    },
    "请输入"
);

//获取值，如果值为提示文字，则返回空值
function get_value(name,cue){
    var obj = (typeof (name) == "string") ? $("input[name='" + name + "']")    : name;
    var value = obj.val();
    return value == cue ? '' : value;
}

$(document).ready(function () {
    $("#collection").validate({
        rules: {
            //定义验证规则
            title: { required_cue: "标题", maxlength: 500 },
            content: { maxlength: 5000 },
            nickname: { maxlength: 50 },
            tel: { maxlength: 50 },
            email: { maxlength: 50 },
            qq: { maxlength: 50 },
            address: { maxlength: 255 },
            company: { maxlength: 255 },
            ext1: { maxlength: 255 },
            ext2: { maxlength: 255 },
            ext3: { maxlength: 255 },
            ext4: { maxlength: 255 },
            ext5: { maxlength: 255 },
            verify_code: { 
                required_cue: "验证码", 
                remote: { 
                    url: url_check_verify, 
                    type: "get"
                }
            }
        },
        submitHandler: function (form) {
            //验证通过后使用ajax保存
            $.post(
                url_guestbook_save,
                {
                    title: get_value('title',''),
                    content: get_value($("textarea[name='content']"),'内容'),
                    nickname: get_value('nickname','称呼'),
                    sex: get_value($("input[name='sex']:checked"),''),
                    tel: get_value('tel','联系电话'),
                    email: get_value('email','电子邮箱'),
                    qq: get_value('qq','QQ号码'),
                    address: get_value('address','联系地址'),
                    company: get_value('company','公司/单位'),
                    ext1: get_value('ext1','备用字段1'),
                    ext2: get_value('ext2','备用字段2'),
                    ext3: get_value('ext3','备用字段3'),
                    ext4: get_value('ext4','备用字段4'),
                    ext5: get_value('ext5','备用字段5'),
                    verify_code: get_value('verify_code','')
                },
                function (data, textStatus) {
                    if (data.err==0) {
                        alert('保存成功！');
                        window.location.href = window.location.href;
                    }else{
                        alert(data.msg);                        
                    }
                },
                "json"
            );
        }
    });
});

